package com.pssmobile.genericglossary.visual;

import com.pssmobile.genericglossary.common.ITermsNavigator;
import com.pssmobile.genericglossary.common.ITermsNavigatorListener;
import com.pssmobile.genericglossary.common.TermsNavigator;
import com.pssmobile.genericglossary.data.DataAdapter;
import com.pssmobile.genericglossary.data.Term;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class DetailsForm extends MainScreen implements ITermsNavigatorListener, FieldChangeListener {

    static Bitmap getBgBitmap() {
        Bitmap bmp;
        if (Display.getWidth() > 360) {
            bmp = Bitmap.getBitmapResource("res/bg2.jpg");
        } else {
            bmp = Bitmap.getBitmapResource("res/bg.jpg");
        }
        return bmp;
    }
    
    Bitmap _bmp;
    Background _bg;
    Background _bgTransparent;
    
    private ITermsNavigator _termsNavigator;
    private int _termID;

    
    private LabelField _labelField;
    private RichTextField _detailsField;
    private ButtonField _prevBtnField;
    private ButtonField _nextBtnField;
    private ButtonField _randomBtnField;
    
    private MenuItem nextTermCmd = new MenuItem("Next", 100, 100) {
        public void run() {
            handleNextTerm();
        }
    };
    private MenuItem prevTermCmd = new MenuItem("Prev", 110, 100) {
        public void run() {
            handlePrevTerm();
        }
    };
    private MenuItem randomTermCmd = new MenuItem("Random", 120, 100) {
        public void run() {
            handleRandomTerm();
        }
    };

    public DetailsForm( int termID ) {
        super();
        _init();
        
        _bmp = getBgBitmap();
        _bg = BackgroundFactory.createBitmapBackground( _bmp, Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_BOTH );;
        _bgTransparent = BackgroundFactory.createSolidTransparentBackground(0xffffff, 0);
        
        getMainManager().setBackground(_bgTransparent);        
        
        _labelField = new LabelField();
        _labelField.setFont( Font.getDefault().derive(Font.BOLD, Font.getDefault().getHeight() + 4 ));
        _labelField.setBackground(_bgTransparent);
        
        _detailsField = new RichTextField();
        _detailsField.setBackground(_bgTransparent);
        
        LabelField title = new LabelField("Definition", FIELD_HCENTER | DrawStyle.HCENTER);
        //title.setBackground(_bgTransparent);
        setTitle( title );
        
        add(_labelField);
        add(new SeparatorField());
        add(_detailsField);
        
        HorizontalFieldManager hfm = new HorizontalFieldManager();
        hfm.setBackground(_bgTransparent);
        hfm.add( _prevBtnField = new ButtonField("Prev", FIELD_HCENTER | ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY) );
        hfm.add( _randomBtnField = new ButtonField("Random", FIELD_HCENTER | ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY) );
        hfm.add( _nextBtnField = new ButtonField("Next", FIELD_HCENTER | ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY) );
        
        _prevBtnField.setChangeListener(this);
        _randomBtnField.setChangeListener(this);
        _nextBtnField.setChangeListener(this);
        
        setStatus(hfm);
        
        loadTerm(termID);
    }
    
    private void _init() {
        _termsNavigator = TermsNavigator.getInstance();
        _termsNavigator.addListener(this);
    }

    // paint custom background
    protected void paintBackground(Graphics g)
    {
        _bg.draw(g, new XYRect(0, 0, getWidth(), _bmp.getHeight()));
    }
    
    /**
     * Handle to build custom menu     
     */
    public void makeMenu(Menu menu, int instance) {    
        super.makeMenu(menu, instance);        
        menu.add(nextTermCmd);
        menu.add(prevTermCmd);
        menu.add(randomTermCmd);
    }

    public boolean onClose() {
        _termsNavigator.removeListener(this);
        return super.onClose();
    }
    
    /**
     * Load & show details of specified term
     * @param termID
     */
    private void loadTerm( int termID ) {
        _termID = termID;
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
                try {
                    Term term = DataAdapter.getInstance().getTermById(_termID);            
                    _labelField.setText(term.name);
                    _detailsField.setText(term.descr);   
                } catch (Exception e) {
                    _labelField.setText("<No title>");
                    _detailsField.setText("<Error getting details>");
                }
                DetailsForm.this.getMainManager().setVerticalScroll(0);
                DetailsForm.this.invalidate();
            }
        });
    }
    
    private void handleNextTerm() {
        _termsNavigator.goNextTerm();
    }
    
    private void handlePrevTerm() {
        _termsNavigator.goPrevTerm();
    }
    
    private void handleRandomTerm() {
        _termsNavigator.goRandomTerm();
    }
    
    
    // ITermsNavigatorListener implementation
    //
    public void currentTermChanged(int termID) {
        loadTerm( termID );
    }

    // FieldChangeListener implementation
    //
    public void fieldChanged(Field field, int context) {
        ButtonField buttonField = (ButtonField) field;
        if (_prevBtnField == buttonField) {
            handlePrevTerm();
        } else if (_nextBtnField == buttonField) {
            handleNextTerm();
        } else if (_randomBtnField == buttonField) {
            handleRandomTerm();
        }
    }
}
